﻿using System; 
using System.Diagnostics;
using System.Reflection;
using System.Runtime.CompilerServices;

namespace SX.Utils
{
    /// <summary>
    /// Utilities for dealing with <see cref="StackTraceUsage"/> values.
    /// </summary>
    public static class StackTraceHelper
    {
        [MethodImpl(MethodImplOptions.NoInlining)]
        public static string GetClassFullName(int framesToSkip = 2)
        { 
            string className = string.Empty; 
            Type declaringType;

            do
            { 
                StackFrame frame = new StackFrame(framesToSkip, false); 
                MethodBase method = frame.GetMethod();
                declaringType = method.DeclaringType;
                if (declaringType == null)
                {
                    className = method.Name;
                    break;
                } 
                framesToSkip++;
                className = declaringType.FullName;
            } while (className.StartsWith("System.", StringComparison.Ordinal)); 
            return className;
        }
   }
}
